// pages/tobeTzSecondStep/tobeTzSecondStep.js
let ubt_js = require("../../utils/ubt.js");
let name_js = require("../../utils/ubtButtonName.js");
let common_js = require("../../utils/common.js");
const apiRequest = require("../../utils/apiRequest.js");
let url_js = require("../../utils/url.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectList: ["拍照", "从手机相册选择"],
    hasShopIndex: 0,
    dzInfo: null,
    timeInfo: null,
    addressInfo: null,
    submitInfo: {
      name: '',
      address: '',
      time: '',
      yyzzImgUrl: '',
      score: 0,
      totalScore: 4,
      isNextEnable: false
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);
    //1.禁止转发
    wx.hideShareMenu();
    //2.初始化数据
    this.initData(options);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },

  //options
  initData: function(options) {
    //1.解析数据
    //1.1店主信息
    let dzInfo = common_js.jsonParse(options.dzInfo);
    this.setData({
      dzInfo: dzInfo
    });
  },

  //门店名称
  onShopName: function(e) {
    let tmp = e.detail.content;
    let tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.name.length == 0 && tmp.length > 0) {
      tmpSubmitInfo.score++;
    } else if (tmpSubmitInfo.name.length > 0 && tmp.length == 0) {
      tmpSubmitInfo.score--;
    }
    tmpSubmitInfo.name = tmp;
    if (tmpSubmitInfo.score == this.data.submitInfo.totalScore) {
      tmpSubmitInfo.isNextEnable = true;
    } else {
      tmpSubmitInfo.isNextEnable = false;
    }
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //选择省市、市辖区
  onMdAddress: function(e) {
    wx.navigateTo({
      url: '../tobeTzEditAddress/tobeTzEditAddress'
    });
  },

  //更新门店地址
  updateAddress: function(res) {
    let tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.address.length == 0) {
      tmpSubmitInfo.score++;
      if (tmpSubmitInfo.score == this.data.submitInfo.totalScore) {
        tmpSubmitInfo.isNextEnable = true;
      } else {
        tmpSubmitInfo.isNextEnable = false;
      }
    }
    tmpSubmitInfo.address = res.ss + '/' + res.sxs + '/' + res.district + res.sxs + '/' + res.street + '/' + res.detailAddress;
    this.setData({
      submitInfo: tmpSubmitInfo,
      addressInfo: res
    });
  },

  //更新营业时间
  updateOpenTime: function(res) {
    let tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.time.length == 0) {
      tmpSubmitInfo.score++;
      if (tmpSubmitInfo.score == this.data.submitInfo.totalScore) {
        tmpSubmitInfo.isNextEnable = true;
      } else {
        tmpSubmitInfo.isNextEnable = false;
      }
    }
    tmpSubmitInfo.time = res.begin + '-' + res.end;
    this.setData({
      submitInfo: tmpSubmitInfo,
      timeInfo: res
    });
  },

  //营业时间
  onShopTime: function(e) {
    wx.navigateTo({
      url: '../tobeTzEditOpenTime/tobeTzEditOpenTime'
    });
  },

  //选择是否有门店
  onShopSelect: function(e) {
    let index = parseInt(e.currentTarget.dataset.index);
    if (index == this.data.hasShopIndex) return;
    //判断下一步是否可以点击
    let tmpSubmitInfo = this.data.submitInfo;
    if (index == 0) {
      tmpSubmitInfo.totalScore = 4;
    } else if (index == 1) {
      tmpSubmitInfo.totalScore = 3;
      if (tmpSubmitInfo.yyzzImgUrl.length > 0) {
        tmpSubmitInfo.score--;
        tmpSubmitInfo.yyzzImgUrl = '';
      }
    }
    if (tmpSubmitInfo.score == this.data.submitInfo.totalScore) {
      tmpSubmitInfo.isNextEnable = true;
    } else {
      tmpSubmitInfo.isNextEnable = false;
    }
    this.setData({
      hasShopIndex: index,
      submitInfo: tmpSubmitInfo
    });
  },

  //上传照片
  selectPic: function() {
    let that = this;
    common_js.showActionSheet(this.data.selectList, function(res) {
      //handle data 
      if (res.tapIndex == 0) {
        that.takePhoto();
      } else if (res.tapIndex == 1) {
        that.chooseFomeAlbum();
      }
    });
  },

  //重新上传
  reloadPic: function() {
    let that = this;
    common_js.showActionSheet(this.data.selectList, function(res) {
      //handle data 
      if (res.tapIndex == 0) {
        that.takePhoto();
      } else if (res.tapIndex == 1) {
        that.chooseFomeAlbum();
      }
    });
  },

  //拍照
  takePhoto: function() {
    wx.chooseImage({
      count: 1,
      sourceType: ['camera'],
      success: res => {
        const tempFilePaths = res.tempFilePaths;
        this.requestToUploadFile(tempFilePaths[0]);
      },
      fail: res => {
        let canceled = res.errMsg.indexOf("cancel") != -1;
        if (!canceled) {
          common_js.showToast(res.errMsg);
        }
      }
    });
  },

  //相册
  chooseFomeAlbum: function() {
    wx.chooseImage({
      count: 1,
      sourceType: ['album'],
      success: res => {
        const tempFilePaths = res.tempFilePaths;
        this.requestToUploadFile(tempFilePaths[0]);
      },
      fail: res => {
        let canceled = res.errMsg.indexOf("cancel") != -1;
        if (!canceled) {
          common_js.showToast(res.errMsg);
        }
      }
    });
  },

  // 上传图片地址
  requestToUploadFile(fielPath) {
    apiRequest.uploadFile(url_js.fileUploadUrl, fielPath).then(res => {
      let resData = JSON.parse(res);
      this.uploadYyzzUrl(resData.result);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //更新营业执照url
  uploadYyzzUrl: function(yyzzUrl) {
    //判断下一步是否可以点击
    let tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.yyzzImgUrl.length == 0 && this.data.hasShopIndex == 0) {
      tmpSubmitInfo.score++;
      if (tmpSubmitInfo.score == this.data.submitInfo.totalScore) {
        tmpSubmitInfo.isNextEnable = true;
      } else {
        tmpSubmitInfo.isNextEnable = false;
      }
    }
    tmpSubmitInfo.yyzzImgUrl = yyzzUrl;
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //下一步
  onNext: function() {
    //1.是否可以点击下一步
    if (!this.data.submitInfo.isNextEnable) return;
    //2.门店名称
    if (this.data.submitInfo.name.length == 0) {
      common_js.showToast('请输入门店名称!');
      return;
    }
    //3.门店地址
    if (this.data.submitInfo.address.length == 0) {
      common_js.showToast('请选择门店地址!');
      return;
    }
    //4.营业时间
    if (this.data.submitInfo.time.length == 0) {
      common_js.showToast('请输入选择营业时间!');
      return;
    }
    //5.营业执照上传
    if (this.data.hasShopIndex == 0 && this.data.submitInfo.yyzzImgUrl.length == 0) {
      common_js.showToast('请上传营业执照!');
      return;
    }

    let mdInfo = {}
    mdInfo.dzInfo = this.data.dzInfo;
    mdInfo.addressInfo = this.data.addressInfo;
    mdInfo.timeInfo = this.data.timeInfo;
    mdInfo.mdInfo = this.data.submitInfo;
    mdInfo.hasShopIndex = this.data.hasShopIndex;
    wx.navigateTo({
      url: '../tobeTzThridStep/tobeTzThridStep?mdInfo=' + common_js.jsonStringify(mdInfo)
    })

    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.tobeTzSecondStepDone);

  }
})